package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.BeamyardTensioningMachineBo;
import com.ruoyi.zhgd.domain.vo.BeamyardTensioningMachineVo;
import com.ruoyi.zhgd.domain.BeamyardTensioningMachine;
import com.ruoyi.zhgd.mapper.BeamyardTensioningMachineMapper;
import com.ruoyi.zhgd.service.IBeamyardTensioningMachineService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 梁场_张拉机信息Service业务层处理
 *
 * @author zyb
 * @date 2023-09-11
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class BeamyardTensioningMachineServiceImpl implements IBeamyardTensioningMachineService {

    private final BeamyardTensioningMachineMapper baseMapper;

    /**
     * 查询梁场_张拉机信息
     */
    @Override
    public BeamyardTensioningMachineVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询梁场_张拉机信息列表
     */
    @Override
    public TableDataInfo<BeamyardTensioningMachineVo> queryPageList(BeamyardTensioningMachineBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BeamyardTensioningMachine> lqw = buildQueryWrapper(bo);
        Page<BeamyardTensioningMachineVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询梁场_张拉机信息列表
     */
    @Override
    public List<BeamyardTensioningMachineVo> queryList(BeamyardTensioningMachineBo bo) {
        LambdaQueryWrapper<BeamyardTensioningMachine> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BeamyardTensioningMachine> buildQueryWrapper(BeamyardTensioningMachineBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BeamyardTensioningMachine> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getDeviceName()), BeamyardTensioningMachine::getDeviceName, bo.getDeviceName());
        lqw.eq(StringUtils.isNotBlank(bo.getDeviceCode()), BeamyardTensioningMachine::getDeviceCode, bo.getDeviceCode());
        return lqw;
    }

    /**
     * 新增梁场_张拉机信息
     */
    @Override
    public Boolean insertByBo(BeamyardTensioningMachineBo bo) {
        BeamyardTensioningMachine add = BeanUtil.toBean(bo, BeamyardTensioningMachine.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改梁场_张拉机信息
     */
    @Override
    public Boolean updateByBo(BeamyardTensioningMachineBo bo) {
        BeamyardTensioningMachine update = BeanUtil.toBean(bo, BeamyardTensioningMachine.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BeamyardTensioningMachine entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除梁场_张拉机信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
